const int NODE = 450005, N = 505, M = 905;
const int INF = 0x3f3f3f3f;  
struct DLX_accurate {
    int U[NODE], D[NODE], R[NODE], L[NODE], row[NODE], col[NODE];  
    int n, m, size, H[N], S[M], ansd, ans[N];  //save ans
    void init(int n, int m) {  
        this->n = n; this->m = m; ansd = INF;  
        for(int i = 0; i <= m; i++) {  
            S[i] = 0; U[i] = D[i] = i;  
            L[i] = i - 1; R[i] = i + 1;  
        }  
        R[m] = 0; L[0] = m; size = m;  
        for(int i = 1; i <= n; i++)  
            H[i] = -1;  
    }  
    void Link(int r, int c) {  
        ++S[col[++size] = c]; row[size] = r;  
        D[size] = D[c]; U[D[c]] = size;  
        U[size] = c; D[c] = size;
        if(H[r] < 0) H[r] = L[size] = R[size] = size;  
        else {  
            R[size] = R[H[r]]; L[R[H[r]]] = size;  
            L[size] = H[r]; R[H[r]] = size;  
        }  
    }  
    void remove(int c) {  
        L[R[c]] = L[c]; R[L[c]] = R[c];  
        for(int i = D[c]; i != c; i = D[i]) {  
            for(int j = R[i]; j != i; j = R[j]) {  
                U[D[j]] = U[j]; D[U[j]] = D[j];  
                --S[col[j]];  
            }  
        }  
    }  
    void resume(int c) {  
        for(int i = U[c]; i != c; i = U[i])  
            for(int j = L[i]; j != i; j = L[j])  
                ++S[col[U[D[j]] = D[U[j]] = j]];  
        L[R[c]] = R[L[c]] = c;  
    }  
    void Dance(int d) {  
        if(ansd <= d) return;  
        if(R[0] == 0) {if(d < ansd) ansd = d; return;}
        int c = R[0];  
        for(int i = R[0]; i != 0; i = R[i]) if(S[i] < S[c]) c = i;  
        remove(c);  
        for(int i = D[c]; i != c; i = D[i]) {  
            for(int j = R[i]; j != i; j = R[j]) remove(col[j]);  
            Dance(d + 1);  
            for(int j = L[i]; j != i; j = L[j]) resume(col[j]);  
        } resume(c);  
    }  
}; 
struct DLX_repeat {
    int U[NODE], D[NODE], R[NODE], L[NODE], row[NODE], col[NODE];  
    int n, m, size, H[N], S[M], ansd, ans[N];  //save ans
    void init(int n,int m) {  
        this->n = n; this->m = m; ansd = INF;  
        for(int i = 0; i <= m; i++) {  
            S[i] = 0; U[i] = D[i] = i;  
            L[i] = i - 1; R[i] = i + 1;  
        }  
        R[m] = 0; L[0] = m; size = m;
        for(int i = 1; i <= n; i++) H[i] = -1;  
    }  
    void Link(int r,int c) {  
        ++S[col[++size] = c]; row[size] = r;
        D[size] = D[c]; U[D[c]] = size;  
        U[size] = c; D[c] = size;  
        if(H[r] < 0) H[r] = L[size] = R[size] = size;  
        else {  
            R[size] = R[H[r]]; L[R[H[r]]] = size;  
            L[size] = H[r]; R[H[r]] = size;  
        }  
    }  
    void remove(int c) {  
        for(int i = D[c]; i != c; i = D[i])
            L[R[i]] = L[i], R[L[i]] = R[i];  
    }  
    void resume(int c) {  
        for(int i = U[c]; i != c; i = U[i])  
            L[R[i]] = R[L[i]] = i;  
    }  
    bool v[NODE];  
    int f() {  
        int ret = 0;  
        for(int c = R[0]; c != 0; c = R[c]) v[c] = true;  
        for(int c = R[0]; c != 0; c = R[c]) {  
            if(v[c]) {  
                ret++; v[c] = false;  
                for(int i = D[c]; i != c; i = D[i])  
                    for(int j = R[i]; j != i; j = R[j])  
                        v[col[j]] = false;  
            }}  
        return ret;  
    }  
    void Dance(int d) {  
        if(d + f() >= ansd) return;  
        if(R[0] == 0) {if (d < ansd) ansd = d;return;}
        int c = R[0];  
        for(int i = R[0]; i != 0; i = R[i]) if(S[i] < S[c]) c = i;  
        for(int i = D[c]; i != c; i = D[i]) {  
            remove(i); for(int j = R[i]; j != i; j = R[j]) remove(j);  
            ans[d] = row[i]; Dance(d + 1);  
            for(int j = L[i]; j != i; j = L[j]) resume(j); resume(i);  
        }  
    }  
} gao;  
